package cn.wolf.notify;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import cn.wolf.notify.NotificationEntity.Message;
import cn.wolf.tools.Log;
import cn.wolf.tools.StringUtil;

/**
 * <h3>消息通知器 </h3>
 * <p>仅在通知栏进行通知操作，是否通知或者开启声音、震动。均由调用方提供。</p>
 */
public class Notifier {

	private Context context;

	private NotificationManager notificationManager;

	public Notifier(Context context) {
		this.context = context;
		this.notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	/**
	 * 判断日期是否在合理范围，以在声音和震动中使用
	 * @return
	 * 		返回结果，boolean
	 */
	private boolean isSoundAndVibrate() {
		return true;
	}

	/**
	 * 进行状态栏消息通知
	 * @param iq
	 * 		通知实体类
	 * @param className
	 * 		调整页面类名
	 */
	public void notify(NotificationIQ iq, String className) {
		Log.d("start notify...");

		int id = iq.getId();
		String title = iq.getTitle();
		String content = iq.getContent();
		Message message = iq.getMessage();
		int icon = iq.getIcon();
		boolean isSound = iq.isSound();
		boolean isVibrate = iq.isVibrate();
		boolean noticeEnable = iq.isEnable();

		Log.d("notificationId=" + id);
		Log.d("notificationTitle=" + title);
		Log.d("notificationMessage=" + content);

		if (noticeEnable) {
			// Show the toast
			// if (NotificationUtil.isNotificationToastEnabled(context)) {
			// Toast.makeText(context, content, Toast.LENGTH_LONG).show();
			// }

			// Notification
			Notification notification = new Notification();
			notification.icon = icon;
			notification.defaults = Notification.DEFAULT_LIGHTS;
			if (isSoundAndVibrate()) {
				if (isSound) {
					notification.defaults |= Notification.DEFAULT_SOUND;
				}
				if (isVibrate) {
					notification.defaults |= Notification.DEFAULT_VIBRATE;
				}
			}

			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notification.when = System.currentTimeMillis();
			notification.tickerText = content;

			Intent intent = new Intent();
			intent.putExtra(NotificationConstants.NOTIFICATION_ID, id);
			intent.putExtra(NotificationConstants.NOTIFICATION_TITLE, title);
			intent.putExtra(NotificationConstants.NOTIFICATION_CONTENT, content);
			intent.putExtra(NotificationConstants.NOTIFICATION_MESSAGE, message);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
			intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

			// 跳转页面
			if (!StringUtil.isEmpty(className)) {
				try {
					Class<?> clazz = Class.forName(className);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.setClass(context, clazz);
				} catch (ClassNotFoundException e) {
					Log.e("调用的类不存在");
				}
			}

			PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent,
					PendingIntent.FLAG_UPDATE_CURRENT);

			notification.setLatestEventInfo(context, title, content, contentIntent);
			notificationManager.notify(id, notification);

		} else {
			Log.w("Notificaitons disabled.");
		}
	}
}
